package com.squeekeek.util.japandict;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class DictSearchPanel extends JPanel {
	
	public DictSearchPanel() {
		JLabel findlbl;
		JTextField searchfld;
		JRadioButton kanabtn;
		JRadioButton romajibtn;
		JRadioButton jpbtn;
		JRadioButton engbtn;
		JButton submitbtn;
		JButton exitbtn;
		JButton updatebtn;
		
		ButtonGroup textbtngrp;
		ButtonGroup langbtngrp;
		
		GroupLayout layout;
		ActionListener myalistener;
		
		findlbl = new JLabel("Find What:");
		searchfld = new JTextField();
		kanabtn = new JRadioButton("Kana/Kanji");
		romajibtn = new JRadioButton("Romaji");
		engbtn = new JRadioButton("English");
		jpbtn = new JRadioButton("Japanese");
		submitbtn = new JButton("Search");
		exitbtn = new JButton("Exit");
		updatebtn = new JButton("Update Dictionary");
		
		textbtngrp = new ButtonGroup();
		textbtngrp.add(kanabtn);
		textbtngrp.add(romajibtn);
		textbtngrp.setSelected(kanabtn.getModel(), true);
		
		langbtngrp = new ButtonGroup();
		langbtngrp.add(jpbtn);
		langbtngrp.add(engbtn);
		langbtngrp.setSelected(jpbtn.getModel(), true);
		
		myalistener = new AListener(searchfld, textbtngrp, langbtngrp);
		exitbtn.setActionCommand("cancel");
		submitbtn.setActionCommand("submit");
		updatebtn.setActionCommand("update");
		kanabtn.setActionCommand("kana");
		romajibtn.setActionCommand("romaji");
		engbtn.setActionCommand("english");
		jpbtn.setActionCommand("japanese");
		
		exitbtn.addActionListener(myalistener);
		submitbtn.addActionListener(myalistener);
		updatebtn.addActionListener(myalistener);
		
		layout = new GroupLayout(this);
		this.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
			.addComponent(findlbl)
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(searchfld)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(kanabtn)
						.addComponent(jpbtn)
					)
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(romajibtn)
						.addComponent(engbtn)
					)
				)
			)
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(submitbtn)
				.addComponent(exitbtn)
			)
		);
		
		layout.setVerticalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				.addComponent(findlbl)
				.addComponent(searchfld)
				.addComponent(submitbtn)
			)
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(kanabtn)
						.addComponent(romajibtn)
					)
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(jpbtn)
						.addComponent(engbtn)
					)
				)
				.addComponent(exitbtn)
			)
		);
	}
}

class AListener implements ActionListener {
	private JTextField searchkeys;
	private ButtonGroup textsel;
	private ButtonGroup langsel;
	
	public AListener(JTextField searchkeys, ButtonGroup textsel, ButtonGroup langsel) {
		this.searchkeys = searchkeys;
		this.textsel = textsel;
		this.langsel = langsel;
	}
	
	public void actionPerformed(ActionEvent ae) {
		RomaKanaConv rkconvobj;
		
		rkconvobj = new RomaKanaConv();
		
		switch(ae.getActionCommand()) {
		case "submit":
			if(this.textsel.getSelection().getActionCommand() == "romaji") {
				String tokanastr = rkconvobj.getKana(searchkeys.getText(), RomaKanaConv.TYPE_HIRA);
				
				JOptionPane.showMessageDialog(null, tokanastr);
			}
			break;
		case "cancel":
			System.exit(0);
			break;
		case "update":
			break;
		}
	}
}